home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Floppyshop 2
/
Floppyshop - 2.zip
/
Floppyshop - 2.iso
/
diskmags
/
3565-4.665
/
dmg-4182
/
a
/
33.pne
< prev
next >
Wrap
Text File
|
1987-04-21
|
6KB
|
227 lines
Speeding Up Your STOS Programs
By Billy Allan
Let's face it, STOS isn't the fastest
of languages. Unfortunately, this is
usually compounded by people not bother-
ing to optimise their programs.
I have seen a great deal of STOS
programs recently which go at an
astoundingly slow rate. Most of them
could be speeded up greatly if the
authors took a little time to try out
various ways of doing things, or even
paused to think about a routine before
they wrote it.
For example, take the following BASIC
list.
10 for T=0 to 9
20 bob logic,start(1),0,X(T),Y(T),0
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),X(T),Y(T),16,16,
16,16) then swap SX(Q),SX(T) : swap SY
(Q),SY(T)
25 next Q
30 X(T)=X(T)+SX(T)
40 if X(T)<0 then X(T)=320
50 if X(T)>320 then X(T)=0
60 Y(T)=Y(T)+SY(T)
70 if Y(T)<0 then Y(T)=200
80 if Y(T)>200 then Y(T)=0
90 next T
100 goto 10
Ok, a fairly contrived loop. But let's
go through it stage by stage and see
what we can find to optimise.
First of all we get replace the "start
(1)" with a variable.
5 S1=start(1)
10 for T=0 to 9
20 bob logic,S1,0,X(T),Y(T),0
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),X(T),Y(T),16,16,
16,16) then swap SX(Q),SX(T) : swap SY
(Q),SY(T)
25 next Q
30 X(T)=X(T)+SX(T)
40 if X(T)<0 then X(T)=320
50 if X(T)>320 then X(T)=0
60 Y(T)=Y(T)+SY(T)
70 if Y(T)<0 then Y(T)=200
80 if Y(T)>200 then Y(T)=0
90 next T
100 goto 10
Now we can also use the MANY BOB
command to draw the sprites.
5 S1=start(1)
10 many bob logic,S1,varptr(IMG(0)),
varptr(X(0)),varptr(Y(0)),varptr(BON
(0)),0,0,0
20 for T=0 to 9
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),X(T),Y(T),16,16,
16,16) then swap SX(Q),SX(T) : swap SY
(Q),SY(T)
25 next Q
30 X(T)=X(T)+SX(T)
40 if X(T)<0 then X(T)=320
50 if X(T)>320 then X(T)=0
60 Y(T)=Y(T)+SY(T)
70 if Y(T)<0 then Y(T)=200
80 if Y(T)>200 then Y(T)=0
90 next T
100 goto 10
Now we can replace those time-consuming
VARPTR's with variables as well.
5 S1=start(1) : VIMG=varptr(IMG(0)) : VX
=varptr(X(0)) : VY=varptr(Y(0)) : VON=
varptr(BON(0))
10 many bob logic,S1,VIMG,VX,VY,VON,0,0
,0
20 for T=0 to 9
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),X(T),Y(T),16,16,
16,16) then swap SX(Q),SX(T) : swap SY
(Q),SY(T)
25 next Q
30 X(T)=X(T)+SX(T)
40 if X(T)<0 then X(T)=320
50 if X(T)>320 then X(T)=0
60 Y(T)=Y(T)+SY(T)
70 if Y(T)<0 then Y(T)=200
80 if Y(T)>200 then Y(T)=0
90 next T
100 goto 10
Which makes the MANY BOB look a damn
sight better as well. Now notice that
the FOR Q..NEXT loop checks the X(T) and
Y(T) variables. But in the Q loop, T is
never changed so we are accessing X(T)
and Y(T) 10 times to get the same value
each time. But as we all know, accessing
a normal variable is faster than access-
ing an array element, so why don't we
store the current X and Y in temporary
variables before entering the loop?
5 S1=start(1) : VIMG=varptr(IMG(0)) : VX
=varptr(X(0)) : VY=varptr(Y(0)) : VON=
varptr(BON(0))
10 many bob logic,S1,VIMG,VX,VY,VON,0,0
,0
20 for T=0 to 9
21 TX=X(T) : TY=Y(T)
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),TX,TY,16,16,16,
16) then swap SX(Q),SX(T) : swap SY(Q)
,SY(T)
25 next Q
30 X(T)=X(T)+SX(T)
40 if X(T)<0 then X(T)=320
50 if X(T)>320 then X(T)=0
60 Y(T)=Y(T)+SY(T)
70 if Y(T)<0 then Y(T)=200
80 if Y(T)>200 then Y(T)=0
90 next T
100 goto 10
Notice that we didn't change the SX/Y
(T)'s. This is because in this parti-
cular case we are unlikely to get many
collisions actually happening. In diff-
erent programs it's always worth check-
ing to see whether a given variable is
worth storing. Now that we have the X
and Y as variables we would be as well
changing all the other occuences in the
loop.
5 S1=start(1) : VIMG=varptr(IMG(0)) : VX
=varptr(X(0)) : VY=varptr(Y(0)) : VON=
varptr(BON(0))
10 many bob logic,S1,VIMG,VX,VY,VON,0,0
,0
20 for T=0 to 9
21 TX=X(T) : TY=Y(T)
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),TX,TY,16,16,16,
16) then swap SX(Q),SX(T) : swap SY(Q)
,SY(T)
25 next Q
30 TX=TX+SX(T)
40 if TX<0 then TX=320
50 if TX>320 then TX=0
60 TY=TY+SY(T)
70 if TY<0 then TY=200
80 if TY>200 then TY=0
85 X(T)=TX : Y(T)=TY90
90 next T
100 goto 10
Notice that we have to store the new X
and Y back in the array at the end of
the loop in line 85. Now, in this case
we can make use of the MANY ADD command.
This is only applicable in some
instances, but this happens to be one of
them.
5 S1=start(1) : VIMG=varptr(IMG(0)) : VX
=varptr(X(0)) : VY=varptr(Y(0)) : VON=
varptr(BON(0))
6 VSX=varptr(SX(0)) : VSY=varptr(SY(0))
10 many bob logic,S1,VIMG,VX,VY,VON,0,0
,0
15 many add VX,VSX,9,0,320
16 many add VY,VSY,9,0,200
20 for T=0 to 9
21 TX=X(T) : TY=Y(T)
22 for Q=0 to 9
23 if Q=T then goto 25
24 if overlap(X(Q),Y(Q),TX,TY,16,16,16,
16) then swap SX(Q),SX(T) : swap SY(Q)
,SY(T)
25 next Q
90 next T
100 goto 10
So now we have a loop which does exact-
ly the same thing, but about 5 times
faster.
Doing this to your whole game can prod-
uce quite significant results. One has
to bare in mind that arcade games should
not update at more than 4 vbls (that's
50/4 or 12.5 frames a second), 2-3 being
the normal level. If your game is updat-
ing very slowly, then don't just release
it and blame STOS for it being slow - it
is possible to write fast games in STOS,
it just takes a little effort on the
part of the programmers.
So next time you see a STOS game which
takes about a second to respond to a
joystick press don't just think "God,
STOS is awful" - think "God, this prog-
rammer is awful!"
Billy Allan - 1993